package com.jdew.thkj.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jdew.thkj.R;
import com.jdew.thkj.base.BaseActivity;
import com.jdew.thkj.manager.AppConfig;
import com.jdew.thkj.network.RetrofitManager;
import com.jdew.thkj.network.api.RequestApi;
import com.jdew.thkj.utils.MD5Utils;
import com.jdew.thkj.utils.RegexValidateUtils;
import com.jdew.thkj.utils.ToastUtils;

import butterknife.Bind;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yangzai on 2018/3/16 0016.
 */

public class BindShangJiActivity extends BaseActivity {
    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.name)
    TextView name;
    @Bind(R.id.phone)
    EditText phone;
    @Bind(R.id.bind)
    TextView bind;
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private String seceret;
    private String cookie;

    @Override
    public int bindLayout() {
        return R.layout.activity_bind_shangji;
    }

    @Override
    public void initView(View view) {
        back.setVisibility(View.VISIBLE);
        title.setText("绑定上级");
        sp= getSharedPreferences("user", Activity.MODE_PRIVATE);
        editor = sp.edit();
        getSessionId();
        back.setOnClickListener(this);
        bind.setOnClickListener(this);
        phone.addTextChangedListener(textWatcher);
    }

    TextWatcher textWatcher = new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (!TextUtils.isEmpty(phone.getText()) && RegexValidateUtils.checkCellphone(phone.getText().toString())) {
                bind.setBackgroundResource(R.drawable.edittext_red_style);
                GetBoosPhone();
            } else {
                bind.setBackgroundResource(R.drawable.edittext_style_dark);
                bind.setEnabled(false);
                name.setVisibility(View.GONE);
            }
        }
    };


    public void GetBoosPhone() {
        String timesamp=RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).GetBoosPhone(cookie,phone.getText().toString().trim(),sp.getString("userPhone",""), RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"BoosPhone"+phone.getText().toString().trim()+"UserPhone"+sp.getString("userPhone",""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(JSONObject json) {
                if (json.get("code").equals("0")) {
                    name.setVisibility(View.VISIBLE);
                    String Name = json.getString("data");
                    name.setText(Name);
                    bind.setBackgroundResource(R.drawable.edittext_red_style);
                    bind.setEnabled(true);
                } else if (json.get("code").equals("-1")) {
                    bind.setEnabled(false);
                    bind.setBackgroundResource(R.drawable.edittext_style_dark);
                    ToastUtils.getInstance(BindShangJiActivity.this).showMessage(json.getString("message"));
                }else if (json.get("code").equals("-2")) {
                    bind.setEnabled(false);
                    bind.setBackgroundResource(R.drawable.edittext_style_dark);
                    ToastUtils.getInstance(BindShangJiActivity.this).showMessage(json.getString("message"));
                }else if (json.get("code").equals("-3")) {
                    bind.setEnabled(false);
                    bind.setBackgroundResource(R.drawable.edittext_style_dark);
                    ToastUtils.getInstance(BindShangJiActivity.this).showMessage(json.getString("message"));
                }
            }
        });
    }
    public void SetBoosPhone() {
        String timesamp=RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).SetBoosPhone(cookie,phone.getText().toString().trim(),sp.getString("userPhone",""),RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken","")+"BoosPhone"+phone.getText().toString().trim()+"UserPhone"+sp.getString("userPhone",""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(JSONObject json) {
                ToastUtils.getInstance(BindShangJiActivity.this).showMessage(json.getString("message"));
                if (json.get("code").equals("0")) {
                    editor.putString("UserSjPhone",phone.getText().toString().trim());
                    editor.apply();
                    Intent in=new Intent();
                    setResult(RESULT_OK,in);
                    finish();
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back: {
                finish();
            }
            break;
            case R.id.bind: {
               SetBoosPhone();
            }
            break;
            default:
                break;
        }
    }
    public void getSessionId() {
        String timesamp=RegexValidateUtils.getTime13();
        String numsamp=RegexValidateUtils.getRandom10();
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).GetSessionId(RegexValidateUtils.getUniquePsuedoID(),timesamp,numsamp,MD5Utils.getMD5(RegexValidateUtils.ASCIISort(timesamp+numsamp+RegexValidateUtils.getUniquePsuedoID()+sp.getString("SignToken",""))).toUpperCase()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(JSONObject json) {
                if (json.get("code").equals("0")) {
                    seceret = json.getString("data");
                    cookie = json.getString("message");
                    editor.putString("seceret",seceret);
                    editor.apply();
                }
            }
        });
    }
}
